﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace ValidControl
{
	/// <summary>
	/// Summary description for ValidationSummaryForm.
	/// </summary>
	public class ValidationSummaryForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox validationErrorsList;
		private System.Windows.Forms.Label lblErrorMessage;
		private System.Windows.Forms.Label label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ValidationSummaryForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.validationErrorsList = new System.Windows.Forms.ListBox();
			this.lblErrorMessage = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// validationErrorsList
			// 
			this.validationErrorsList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.validationErrorsList.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.validationErrorsList.IntegralHeight = false;
			this.validationErrorsList.Location = new System.Drawing.Point(8, 32);
			this.validationErrorsList.Name = "validationErrorsList";
			this.validationErrorsList.Size = new System.Drawing.Size(272, 80);
			this.validationErrorsList.TabIndex = 1;
			this.validationErrorsList.DoubleClick += new System.EventHandler(this.validationErrorsList_DoubleClick);
			// 
			// lblErrorMessage
			// 
			this.lblErrorMessage.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lblErrorMessage.BackColor = System.Drawing.Color.Transparent;
			this.lblErrorMessage.Location = new System.Drawing.Point(8, 8);
			this.lblErrorMessage.Name = "lblErrorMessage";
			this.lblErrorMessage.Size = new System.Drawing.Size(240, 16);
			this.lblErrorMessage.TabIndex = 2;
			this.lblErrorMessage.Text = "[ErrorMessage Goes Here]";
			// 
			// label1
			// 
			this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.label1.BackColor = System.Drawing.Color.Transparent;
			this.label1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(8, 128);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(240, 16);
			this.label1.TabIndex = 3;
			this.label1.Text = "Kích đôi vào lỗi để sửa lỗi liên quan.";
			// 
			// ValidationSummaryForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.BackColor = System.Drawing.Color.WhiteSmoke;
			this.ClientSize = new System.Drawing.Size(290, 150);
			this.Controls.Add(this.validationErrorsList);
			this.Controls.Add(this.lblErrorMessage);
			this.Controls.Add(this.label1);
			this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ValidationSummaryForm";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "[ErrorCaption Goes Here]";
			this.Load += new System.EventHandler(this.ValidationSummaryForm_Load);
			this.ResumeLayout(false);

		}
		#endregion
    
		public string ErrorCaption 
		{
			get { return this.Text; }
			set { this.Text = value; }
		}
    
		public string ErrorMessage 
		{
			get { return this.lblErrorMessage.Text; }
			set { this.lblErrorMessage.Text = value; }
		}
    
		public void LoadValidators(ValidatorCollection validators) 
		{
    
			// Note: validators should contain all validators under
			// the jurisdiction of the BaseContainerValidator so
			// we can handle both where a control becomes valid and
			// where a control becomes invalid. If we only get 
			// the invalid controls, we can't display controls that were
			// valid at the time but become invalid
      
			// If list currently has items then deregister the 
			// Validate event handler
			if( this.validationErrorsList.Items.Count > 0 ) 
			{
				foreach( BaseValidator validator in this.validationErrorsList.Items ) 
				{
					validator.Validated -= new EventHandler(BaseValidator_Validated);
				}
			}
      
			// Clear the list
			this.validationErrorsList.Items.Clear();
      
			// Add new validators and register the Validate 
			// event handler
			foreach(BaseValidator validator in validators ) 
			{
				if( !validator.IsValid ) 
				{
					this.validationErrorsList.Items.Add(validator);
				}
				validator.Validated += new EventHandler(BaseValidator_Validated);
			}
			
			if(validationErrorsList.Items.Count == 0)
			{				
				this.Close();
			}
		}

		private void validationErrorsList_DoubleClick(object sender, System.EventArgs e) 
		{
			try
			{
				// Set focus on the selected BaseValidator's ControlToValidate ie
				// in the owner form
				BaseValidator selected = (BaseValidator)this.validationErrorsList.SelectedItem;
				selected.ControlToValidate.Focus();
				selected.ControlToValidate.BackColor = Color.MistyRose;
			}
			catch
			{				
			}
		}
    
		public void BaseValidator_Validated(object sender, EventArgs e) 
		{
    
			BaseValidator validator = (BaseValidator)sender;
      
			// If validator is valid, remove from list
			if( validator.IsValid ) 
			{
				this.validationErrorsList.Items.Remove(validator);
				validator.ControlToValidate.BackColor = SystemColors.Window;
				if(this.validationErrorsList.Items.Count == 0)
				{
					this.Close();
				}
			}
				// Add validator to list, in tab index order
			else 
			{
				if( !this.validationErrorsList.Items.Contains(validator) ) 
				{
					decimal tabIndex = validator.FlattenedTabIndex;
					for( int i = 0; i < this.validationErrorsList.Items.Count; i++ ) 
					{
						BaseValidator currentValidator = (BaseValidator)this.validationErrorsList.Items[i];
						if( tabIndex < currentValidator.FlattenedTabIndex ) 
						{
							this.validationErrorsList.Items.Insert(i, validator);
							return;
						}
					}
					this.validationErrorsList.Items.Add(validator);
				}
			}
		}

		private void ValidationSummaryForm_Load(object sender, System.EventArgs e) 
		{
			if(validationErrorsList.Items.Count == 0)					
			{
				this.Close();
			}
			else
			{
				validationErrorsList.SelectedIndex = 0;
			}

		}
	}
}
